package com.jx.mlxg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jx.mlxg.common.BusinessException;
import com.jx.mlxg.entity.ApprovalNode;
import com.jx.mlxg.entity.ApprovalRecord;
import com.jx.mlxg.entity.ShippingInfo;
import com.jx.mlxg.entity.Users;
import com.jx.mlxg.mapper.ShippingInfoMapper;
import com.jx.mlxg.service.ApprovalFlowService;
import com.jx.mlxg.service.ShippingInfoService;
import com.jx.mlxg.service.UsersService;
import com.jx.mlxg.utils.ResponseResult;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 审批流
 * */
@Slf4j
@RestController
@RequestMapping("/approval")
public class ApprovalController {

    @Autowired
    private ApprovalFlowService approvalFlowService;
    @Resource
    private UsersService usersService;

    @Resource
    private ShippingInfoMapper shippingInfoMapper;
    /**
     * 启动审批流程
     * */
    // 启动审批流程
    @PostMapping("/start")
    public ResponseResult startApprovalFlow(@RequestParam String orderId, @RequestParam Long userId) {
        approvalFlowService.startApprovalFlow(orderId, userId);
        return ResponseResult.success("审批流程已启动");
    }

    /**
     * 审批操作
     * */
    // 审批操作
    @PostMapping("/approve")
        public ResponseResult approve(@RequestBody Map map) {
        log.info("orderId----->"+map);
        String  orderId = (String) map.get("orderId");
        String expressNumber = (String) map.get("expressNumber");
        String approveStatus = (String) map.get("approveStatus");
        String approveOpinion = (String) map.get("approveOpinion");

        try{
            approvalFlowService.approve(orderId, approveStatus, approveOpinion,false);
            ShippingInfo shippingInfo = new ShippingInfo();
            shippingInfo.setOrderId(orderId);
            shippingInfo.setTrackingNumber(expressNumber);
            shippingInfo.setShippingStatus("已发货");
            shippingInfo.setUpdatedAt(LocalDateTime.now());

            UpdateWrapper<ShippingInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_id", orderId);

            int rowsAffected = shippingInfoMapper.update(shippingInfo, updateWrapper);
            if (rowsAffected > 0) {
                System.out.println("更新成功！");
            } else {
                System.out.println("更新失败，未找到匹配的记录！");
                return ResponseResult.success(false);
            }
            Boolean isSuccess = true;
            return ResponseResult.success(isSuccess);
        }catch (BusinessException e){
            return ResponseResult.failure(e.getCode(), e.getMessage());
        }

    }

    /**
     * 获取审批记录
     * */
    // 获取审批记录
    @GetMapping("/records")
    public ResponseResult<ApprovalRecord> getApprovalRecords(@RequestParam String orderId) {
       ApprovalRecord records = approvalFlowService.getApprovalRecords(orderId);
        return ResponseResult.success(records);
    }

    /**
     * 获取当前审批节点
     * */
    // 获取当前审批节点
    @GetMapping("/current-node")
    public ResponseResult<ApprovalNode> getCurrentNode(@RequestParam String orderId) {
        ApprovalNode currentNode = approvalFlowService.getCurrentNode(orderId);
        return ResponseResult.success(currentNode);
    }
}